<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Adminer;

use App\Utils\ArrUtil;
use Carbon\Carbon;
use App\Models\Adminer\RoleMenu;
use App\Models\Eloquent;

/**
 * Class Menus
 *
 * @property int $id
 * @property int $pid
 * @property string $api
 * @property string $display_name
 * @property string $name
 * @property string $icon
 * @property string $path
 * @property int $is_menu
 * @property int $sort
 * @property array|null $param
 * @property int $has_line
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class Menus extends Eloquent
{
	protected $table = 'menus';

	protected $casts = [
		'pid' => 'int',
		'is_menu' => 'int',
		'sort' => 'int',
		'param' => 'json',
		'has_line' => 'int'
	];

	protected $fillable = [
		'pid',
		'api',
		'display_name',
		'name',
		'icon',
		'path',
		'is_menu',
		'sort',
		'param',
		'has_line'
	];


    public static function getMenusByRoleId($RoleId = '', $tree = true, $is_menu = false)
    {

        $query = self::orderBy('sort')->orderBy('id');
        if($RoleId) {
            $menu_ids = RoleMenu::getByRoleId($RoleId);
            $query = $query->whereIn('id', $menu_ids);
        }

        if($is_menu) {
            $query->where('api', '/');
        }

        $list = $query->get();

        return $tree ? list2ree($list->toArray()) : $list;
    }
}
